package com.qfedu.edu.iotserver.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qfedu.edu.exception.handler.BusinessException;
import com.qfedu.edu.pojo.Tenant;
import com.qfedu.edu.iotserver.mapper.TenantMapper;
import com.qfedu.edu.iotserver.service.TenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.edu.result.ResponseCode;
import com.qfedu.edu.vo.TenantAuditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.edu.exception.handler.BusinessException;
import com.qfedu.edu.iotserver.mapper.TenantMapper;
import com.qfedu.edu.iotserver.service.TenantService;
import com.qfedu.edu.iotserver.utils.SnowflakeIdGenerator;
import com.qfedu.edu.pojo.Tenant;
import com.qfedu.edu.result.ResponseCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;


/**
 * <p>
 * 租户表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2024-09-13
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(Tenant tenant) {
        //首先校验参数合法性
        if(null==tenant || StringUtils.isEmpty(tenant.getName()) || StringUtils.isEmpty(tenant.getPwd())
                || StringUtils.isEmpty(tenant.getCompanyName()) || StringUtils.isEmpty(tenant.getCompanyCode())
                || StringUtils.isEmpty(tenant.getDescription()) || StringUtils.isEmpty(tenant.getPhone())){
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION.getCode(),ResponseCode.PARAMETER_EXCEPTION.getMsg());
        }

        tenant.setEnable(1);

        synchronized (this){
            long l = new SnowflakeIdGenerator(1L, 1L).nextId();
            tenant.setAccessKey(String.valueOf(l));
            tenant.setAccessSecurit(UUID.randomUUID().toString().replace("-", "").trim());
        }

        tenantMapper.insert(tenant);
    }


    @Transactional(rollbackFor = Exception.class)//事务注解，用于事务控制,如果出现异常，则回滚事务
    @Override
    public void audit(TenantAuditVo tenantAuditVo) {
        // 校验参数合法性
        if (null == tenantAuditVo || StringUtils.isEmpty(tenantAuditVo.getTenantId()) || null == tenantAuditVo.getAuditStatus()) {
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION.getCode(),
                    ResponseCode.PARAMETER_EXCEPTION.getMsg());
        }
        //执行到这里说明参数合法
        //根据租户ID查询租户信息
        Tenant tenant = tenantMapper.selectById(tenantAuditVo.getTenantId());
        if (null == tenant) {
            throw new BusinessException(ResponseCode.TENANT_ID_EXCEPTION.getCode(),
                    ResponseCode.TENANT_ID_EXCEPTION.getMsg());
        }
        //更新租户审核状态
        tenant.setAuditStatus(tenantAuditVo.getAuditStatus());
        //执行到这里说明租户ID存在，参数无误
        //更新租户信息
        tenantMapper.updateById(tenant);

    }

}
